namespace SimpleWebServices.Web.UI.Html
{
    using System;
    using System.Xml.Serialization;
    using SimpleWebServices;
    using Xunit;

    public sealed class ButtonFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<Button>()
                .DerivesFrom<Control>()
                .IsAbstractBaseClass()
                .Implements<IAttributeAccesskey>()
                .Implements<IXmlSerializable>()
                .XmlRoot("button")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            Button expected = new ButtonDerived(new Name("id"))
            {
                Class = "class",
                Accesskey = 'a',
                Disabled = true,
                Language = "fr",
                TabIndex = 1,
                Title = "title",
                Value = "value"
            };

            string xml = "<button id='id' class='class' lang='fr' name='id' accesskey='a' tabindex='1' title='title' disabled='disabled' type='derived' value='value' />";
            Button actual = xml.Deserialize<ButtonDerived>();

            Assert.Equal<string>(expected.ToString(), actual.ToString());
        }

        [Fact]
        public void serialize()
        {
            Button obj = new ButtonDerived(new Name("id"))
            {
                Class = "class",
                Accesskey = 'a',
                Disabled = true,
                Language = "fr",
                TabIndex = 1,
                Title = "title",
                Value = "value"
            };

            string xpath = "1=count(/button[@id='id'][@class='class'][@lang='fr'][@title='title'][@name='id'][@tabindex='1'][@disabled='disabled'][@accesskey='a'][@type='derived'][@value='value'])";

            Assert.True((bool)obj.Serialize().CreateNavigator().Evaluate(xpath));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new ButtonDerived() as Button);
        }

        [Fact]
        public void ctor_Name()
        {
            Assert.NotNull(new ButtonDerived(new Name("id")) as Button);
        }

        [Fact]
        public void ctor_NameNull()
        {
            Assert.NotNull(new ButtonDerived(null) as Button);
        }

        [Fact]
        public void prop_Accesskey()
        {
            Assert.True(new PropertyExpectations(typeof(ButtonDerived).GetProperty("Accesskey"))
                .IsAutoProperty<char?>()
                .Result);
        }

        [Fact]
        public void prop_Type()
        {
            Assert.True(new PropertyExpectations(typeof(ButtonDerived).GetProperty("Type"))
                .DefaultValueIs("derived")
                .Result);
        }

        [Fact]
        public void prop_Value()
        {
            Assert.True(new PropertyExpectations(typeof(ButtonDerived).GetProperty("Value"))
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void IXmlSerializable_op_ReadXml_XmlReaderInvalidType()
        {
            string xml = "<button type='invalid' />";

            Assert.Throws<InvalidOperationException>(() => xml.Deserialize<ButtonDerived>());
        }
    }
}